package com.example.jc.xiao

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.jc.Message
import com.example.jc.R
import com.example.jc.SampleData
import com.example.jc.ui.theme.JComposeAppTheme

/**
 * 参考模板
 */
class MyComposeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JComposeAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
//                    Greeting("Android")
                    Conversation(SampleData().messages)
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Row(//水平排列项
        // 设置Column的宽度高200dp
        modifier = Modifier
            .height(100.dp)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly

    ) {
        Box(//堆叠元素
            modifier = Modifier
                .background(Color.Red)
                .height(80.dp)
                .weight(1f),
            contentAlignment = Alignment.Center
        ) {
            Image(
                painter = painterResource(R.drawable.profile_picture),
                contentDescription = "Contact profile picture",

                modifier = Modifier
                    // Set image size to 80 dp
                    .size(80.dp)
                    // Clip image to be shaped as a circle
                    .clip(CircleShape)
                    .border(1.5.dp, MaterialTheme.colors.secondary, CircleShape)

            )
//            Text(text = "Hello Box!")
//            Text(text = "Hello1 Box!", modifier = Modifier.offset(x = 40.dp, y = 40.dp))
        }
        Spacer(//空白/分割线
            //Add a horizontal space between the image and the column
            //Add a vertical space between the author and message texts
            modifier = Modifier
                .width(8.dp)
                .background(Color.Blue)
        )
        // We keep track if the message is expanded or not in this
        // variable
        var isExpanded by remember { mutableStateOf(false) }
        // surfaceColor will be updated gradually from one color to the other
        val surfaceColor: Color by animateColorAsState(
            if (isExpanded) MaterialTheme.colors.primary else MaterialTheme.colors.surface,
        )

        // We toggle the isExpanded variable when we click on this Column
        Column(//垂直排列元素
            modifier = Modifier
                .weight(1f)
                .fillMaxHeight()
                .clickable { isExpanded = !isExpanded },//点击事件
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Hello $name!",
                color = MaterialTheme.colors.secondaryVariant,
                style = MaterialTheme.typography.subtitle2
            )

            Spacer(modifier = Modifier.height(4.dp))

            Surface(
                shape = MaterialTheme.shapes.medium, elevation = 1.dp,
                // surfaceColor color will be changing gradually from primary to surface
                color = surfaceColor,
                // animateContentSize will change the Surface size gradually
                modifier = Modifier
                    .animateContentSize()
                    .padding(1.dp)

            ) {
                Text(
//                    text = "Hello1 $name!",
                    text = "If the message is expanded, we display all its contentIf the message is expanded, we display all its content ",
                    modifier = Modifier.padding(all = 4.dp),
                    // If the message is expanded, we display all its content
                    // otherwise we only display the first line
                    maxLines = if (isExpanded) Int.MAX_VALUE else 1,
                    style = MaterialTheme.typography.body2
                )
            }
//            MessageCard(Message("Android", "Jetpack Compose"))
        }
    }
}
@Composable
fun Conversation(messages: List<Message>) {

    // We save the scrolling position with this state that can also
    // be used to programmatically scroll the list
    val scrollState = rememberLazyListState()

    LazyColumn(state = scrollState) {
        items(messages) { message ->
//            MessageCard(message)
            Greeting(message.author)
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
    JComposeAppTheme {
        Conversation(SampleData().messages)
//        Greeting("Android")
     }
}